package cn.bdqn.web;

import cn.bdqn.entity.Users;
import cn.bdqn.service.Imp.UsersserviceImp;
import cn.bdqn.service.Usersservice;
import cn.bdqn.utils.Jsonutils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 梁非凡
 * @date 2018/8/13 16:50
 */
@WebServlet(value ={"/Login"})
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        Map<String,Object> result = new HashMap<>();
        String name = request.getParameter("name");
        String password=request.getParameter("password");
        if (name==null&&name.equals("")){
            result.put("code","404");
            result.put("msg","用户名不存在请重试");
            String jsonResult = Jsonutils.getjson(result);
            response.getWriter().write(jsonResult);
            return;
        }
        Usersservice usersservice=new UsersserviceImp();
        Users users = usersservice.findbyidandname(name, password);
        if (users==null){
            result.put("code","500");
            result.put("msg","用户名或密码错误请重试");
            String jsonResult = Jsonutils.getjson(result);
            response.getWriter().write(jsonResult);
            return;

        }
        Integer staus = users.getStaus();
        if (staus==0){
            result.put("code","300");
            result.put("msg","用户已冻结");
            String jsonResult = Jsonutils.getjson(result);
            response.getWriter().write(jsonResult);
            return;
        }
            result.put("msg","登录成功");
            request.getSession().setAttribute("users",users);
        String jsonResult = Jsonutils.getjson(result);
        response.getWriter().write(jsonResult);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
